package main

import "fmt"

var (
	myChannel   chan int
	exitChannel chan bool
)

func test03() {

	for i := 1; i <= 200; i++ {
		res := 0
		for j := 1; j <= i; j++ {
			res += j
		}
		myChannel <- res
	}

	close(myChannel)

}

func readTest03Value() {
	for value := range myChannel {
		fmt.Printf("value is %d \n", value)
	}
	exitChannel <- true
	close(exitChannel)
}

/**

  发生错误：fatal error: concurrent map writes
  查看资源竞争的情况： -race
  例如：
   go build -race channel_01_err.go
   ././channel_01


解除方案：
  1.全变量加锁
  2.channel

*/
func main() {
	myChannel = make(chan int, 10)
	exitChannel = make(chan bool, 1)
	go test03()
	go readTest03Value()

	for {
		_, ok := <-exitChannel
		if !ok {
			break
		}
	}
}
